#include <chrono>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <map>
#include <sstream>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/optional_debug_tools.h"
#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
#include "tts_lib_intf.h"
#include "predictor/TtsPredictor.h"
using namespace std;
ofstream *wav_ofs;
    //    std::string out_name(std::to_string(_pcmid)+"debugcpp.pcm");
    //    ofstream wav_ofs(out_name, std::ios::binary|std::ios::out);
    //    wav_ofs.write((char*)_pcm.data(),_pcm.size()*2);
void recv_pcm(std::vector<short> &pcm, void* args,bool endf){
    std::cout<<" recv pcm "<<pcm.size()<<" "<<endf<<std::endl;
    if (pcm.size() > 0){
        wav_ofs->write((char*)pcm.data(),pcm.size()*2);
    }
}
int main(int argc, const char* argv[]) {
      if (argc != 5) {
          std::cerr << "usage: example-app <path-to-exported-script-module>\n";
          return -1;
      }

      std::string model_path(argv[1]);
      std::string model_fix(argv[2]);
      stTTSInst st_tts(model_path,model_fix,recv_pcm,NULL);
          
    	const bool tbUpdateDictFlag = false;
    
    	FrontAliVc mFrontAliVc;
    	mFrontAliVc.FrontInit("../front/dictionary/", "../front/dict/", "../front/py2phoneMap.txt", tbUpdateDictFlag);

      std::string file_name(argv[3]);
      ifstream filein(file_name, std::ios::in);
      
      int fidx = 0;
      for (std::string line; std::getline(filein, line); ) 
      {
          fidx += 1;
          char buff[10];
          std::sprintf(buff,"%.2i.raw",fidx);

          std::cout << line << std::endl;

          std::string out_name(argv[4]);
          //ofstream wav_ofs(out_name, std::ios::out);
          out_name = out_name + "/" + std::string(buff);
          wav_ofs = new ofstream(out_name, std::ios::binary|std::ios::out);
    
    	//const string tcInputText = "北京今天天气怎么样呢，明天偶然呢";
    	//const string tcInputText(argv[2]);
    	const string tcInputText(line);
    	const string tcInputLanguage = "zh";
    
    	vector< vector< vector<string> > > tvOutSentenceAliVc;
    
    	bool tFrontRes = mFrontAliVc.FrontProcess(tcInputText, tcInputLanguage, tvOutSentenceAliVc);
    	cout << tvOutSentenceAliVc << endl;
    
          std::chrono::time_point<std::chrono::steady_clock> start,end,finish;
          start = std::chrono::steady_clock::now();
          //st_tts.input(debug_sy);
          st_tts.input(tvOutSentenceAliVc);
          finish = std::chrono::steady_clock::now();
          std::cout<< "proctime(ms) : "<<chrono::duration_cast<chrono::milliseconds>(finish-start).count()<<"\n";
          wav_ofs->close();
    } 
    std::cout<<"init decoder"<<"\n";
    std::cout << "ok\n";
}
